package com.gitee.huxing.course.conc.task;

import java.util.concurrent.CountDownLatch;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.conc.task
 * @ClassName: TaskService01Impl
 * @Author: huxing
 * @DateTime: 2021-08-25 下午4:58
 */
public class TaskThreadPool06 extends AbstractTaskThreadPool{

    public static void main(String[] args) throws InterruptedException{
        new TaskThreadPool06().task();
    }

    @Override
    public int threadTask() {
        final int[] result = new int[1];
        final Object lock = new Object();
        Thread thread1 = new Thread(new CustomRunnable(lock, result));
        try {
            // 执行一个线程
            thread1.start();
            // 等待直到线程结束
            synchronized (lock){
                lock.wait();
            }
        } catch (InterruptedException ex){
            ex.printStackTrace();
        }
        return result[0];
    }

    static class CustomRunnable implements Runnable{

        private Object lock;

        private int[] result;

        public CustomRunnable(Object lock, int[] result){
            this.lock = lock;
            this.result = result;
        }

        @Override
        public void run() {
            result[0] = CourseTask.sum();
            synchronized (lock){
                lock.notify();
            }
        }
    }
}
